<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<%@ page import="hk.hku.hooku.db.*"%>
<%@ page import="hk.hku.hooku.pojo.*" %>
<%@ page import="hk.hku.hooku.util.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.util.Map.*" %>
<%@ page import="java.sql.Timestamp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="js/jquery-1.4.2.min.js" ></script>
<script type="text/javascript" src="js/eventInit.js" ></script>
<%
	String url=request.getParameter("url");
	Event event=EventDB.getFullEventByUrl(url);
%>

<title>Hooku Event</title>


<link rel="stylesheet" type="text/css" media="screen" href="css/screen.css" />

</head>

<body>
<%
	Timestamp closeTime=event.getCloseTime();
	boolean isClose=!TimeUtil.isNullTimestamp(closeTime);
%>
<label id="isClosedLabel" hidden><%=isClose %></label>
<label id="eventUrl"  hidden><%=url %></label>
<!--header -->
<div id="header-wrap"><div id="header">
	<h1 id="logo-text"><a href="eventCreate.jsp" title="">Hooku</a></h1>
	<p id="slogan">To Make the Easiest Arragements </p>

	

<!--/header-->
</div></div>
	
<!-- content-outer -->
<div id="content-wrap" class="clear" >

	<!-- content -->
   <div id="content">

   	<!-- main -->
	   <div id="main">

     
            <div class="post-bottom-section">


            <div class="right">
				<p><h2>Arrangement</h2></p>
            	<form  method="post" id="commentform">

               	<p>
					 		<label for="title">Title (required)</label><br />
							<input id="title" name="title" value="<%=event.getTitle() %>" type="text" tabindex="1" disabled/>
						</p>
                  <p>
						 	<label for="location">Location (required)</label><br />
							<input id="location" name="location" value="<%=event.getLocation() %>" type="text" tabindex="2" disabled/>
						</p>
                  <p>
							<label for="description">Description</label><br />
							<textarea id="description" name="description" rows="4" cols="20" tabindex="3" disabled><%=event.getDescription() %></textarea>
						</p>
				<p class="no-border">
				<label for="votes" >Votes</label><br />
				<div id="voteTable">
				<table >
					<thead>
						<!-- Date slots -->
						<tr>
							<td></td>
							<%
							ArrayList<Entry<Date, ArrayList<EventChoice>>> sortedChoices=event.getSortedeventChoices();
							ArrayList<EventChoice> plainChoices=new ArrayList<EventChoice>();
							for(int i=0;i<sortedChoices.size();i++){
								Entry<Date, ArrayList<EventChoice>> entry=sortedChoices.get(i);
								Date date=entry.getKey();
								ArrayList<EventChoice> choices=entry.getValue();
								for(int j=0;j<choices.size();j++){
									plainChoices.add(choices.get(j));
									if(j==0){%>
										<th><%=TimeUtil.toShortDay(date) %> <%=TimeUtil.toShortMonth(date) %> <%=date.getDate() %>, <%=date.getYear()+1900 %></th>
									<%}else{%>
										<th></th>
									<%}
								}
							}
							ArrayList<EventVote> votes=event.getEventVotes();
							Set<String> mailsTo=EventDB.getMailTo(url);
							%>
							<th></th>
						</tr>
						<!-- Time slots -->
						<tr>
							<td><%=votes.size() %> participants</td>	
							<%
							for(int i=0;i<plainChoices.size();i++){
								
								EventChoice choice=plainChoices.get(i);
								Timestamp startTime=choice.getStartTime();
								Timestamp endTime=choice.getEndTime();
								if(startTime.equals(endTime)){%>
									<th><%=String.format("%02d", startTime.getHours()) %>:<%=String.format("%02d", startTime.getMinutes()) %></th>
								<%}else{%>
									<th><%=String.format("%02d", startTime.getHours()) %>:<%=String.format("%02d", startTime.getMinutes()) %>~<%=String.format("%02d", endTime.getHours()) %>:<%=String.format("%02d", endTime.getMinutes()) %></th>
								<%}
							}
							%>
							<th id="noteHead">Note</th>
						</tr>
						<!-- Total sum up -->
						<tr id="total" hidden>
							<td>Total Choices</td>	
							<%
							
							for(int i=0;i<plainChoices.size();i++){
								
								EventChoice choice=plainChoices.get(i);
							%>
								<td><label id="total<%=choice.getId() %>"></label></td>
							<%
							}
							%>	
							<td></td>
						</tr>
						<!-- Decision Choice -->
						<tr id="decision" hidden>
						<td>Decision</td>
						<%
						ArrayList<EventChoice> resultChoices=event.getEventResult().getChoices();
						ArrayList<Integer> resultChoiceIds=new ArrayList<Integer>();
						for(EventChoice choice: resultChoices){
							resultChoiceIds.add(choice.getId());
						}
						for(int i=0;i<plainChoices.size();i++){		
							EventChoice choice=plainChoices.get(i);
							boolean checked=resultChoiceIds.contains(choice.getId());
							if(checked){%>
								<td class="decisionDiv"><input type="checkbox" class="decisionBox" value=<%=choice.getId() %> checked/></td>
							<%}else{%>
								<td class="decisionDiv"><input type="checkbox" class="decisionBox" value=<%=choice.getId() %> /></td>
							<%}						
						}
						%>
						<td></td>
						</tr>
					</thead>
					<tbody>
						<%
						HashMap<Integer,Integer> map=new HashMap<Integer,Integer>();
						
						for (int i=0;i<votes.size();i++){
							EventVote vote=votes.get(i);
							mailsTo.add(vote.getVoterEmail());
							%>
							<tr>
								<td><%=vote.getVoterEmail() %></td>
								<%
								for(int j=0;j<plainChoices.size();j++){
									
									EventChoice choice=plainChoices.get(j);
									ArrayList<EventChoice> voteChoices=vote.getEventChoices();
									boolean isSelect=false;
									for(EventChoice voteChoice:voteChoices){
										if(voteChoice.getId()==choice.getId()){
											isSelect=true;
											break;
										}
									}
									if(isSelect){ 
										Integer count=map.get(choice.getId());
										if(count==null){
											map.put(choice.getId(), 1);
										}else{
											map.put(choice.getId(), count+1);
										}
									%>
										<td><input type="checkbox" value=<%=choice.getId() %> disabled checked/></td>
									<% }else{%>
										<td><input type="checkbox" value=<%=choice.getId() %> disabled/></td>
									<%}
								}
								%>
								<td><%=vote.getNote() %></td>
							</tr>
							<%
						}
						%>
						<tr  hidden>
							<td>Total Choices</td>	
							<%
							int maxChoice=0;
							for(int i=0;i<plainChoices.size();i++){		
								EventChoice choice=plainChoices.get(i);
								Integer count=map.get(choice.getId());
								if(count==null){
									count=0;
								}
								if(count>maxChoice){
									maxChoice=count;
								}
							%>
								<td><label class="hiddenTotal" id="hiddenTotal<%=choice.getId() %>" ><%=count %></label></td>
							<%
							}
							%>
						</tr>
						<tr hidden>
							<td>
							<%

							if(maxChoice!=0){
								ArrayList<Integer> recommendIds=new ArrayList<Integer>();
								for(int i=0;i<plainChoices.size();i++){		
									EventChoice choice=plainChoices.get(i);
									Integer count=map.get(choice.getId());
									if(count==null){
										count=0;
									}
									if(count==maxChoice){
										recommendIds.add(choice.getId());
									}
								}
								for(int id:recommendIds){%>
									<label class="recommendIdLabel"><%=id %></label>
								<%}
							}
							
							%>
							</td>
						</tr>
					</tbody>
				</table>
				</div>
			</p>
			
			<%
			if(event.getEventResult()!=null){
				String resultNote=event.getEventResult().getNote();
				if(resultNote!=null&&!resultNote.equals("")){
			%>
			<p id="resultNoteDiv">
		 		<label for="resultNote">Note</label><br />
				<input id="resultNote" name="resultNote" value="<%=event.getEventResult().getNote() %>" type="text" tabindex="4" />
			</p>	
			<%	}
			}
			
			StringBuilder mailBuilder=new StringBuilder();
		 	for(String mail:mailsTo){
		 		mailBuilder.append(mail+"\n");
		 	}
			%>
			<p id="mailToP"  hidden>
		 		<label for="mailTo">Mail Addresses</label><br />
				<textarea id="mailTo" name="mailTo" cols="20" rows="4" tabindex="5"><%=mailBuilder.toString() %></textarea>
				
				<input type="button" class="button" value="Send Result" id="sendButton" hidden/>
			</p>
			 <p class="no-border" id="buttonDiv">
	<input type="button" class="button" value="Close Event" id="closeButton" />
	<input type="button" class="button" value="Reopen Event" id="reopenButton" hidden/>
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<input type="button" class="button" value="Save" id="saveButton" hidden/>
					
						</p>
			<p class="no-border" id="messageDiv" hidden></p>
			<p class="no-border" id="errorMsgDiv" hidden></p>
               </form>

            </div>

         </div>

        <!-- /main -->
		</div>

      <!-- sidebar -->
		<div id="sidebar">
			<div class="sidemenu">
				<h3>Poll Link</h3>
				<p>
					<input type="text" value="http://<%=Config.HOST %>/eventVote.jsp?url=<%=event.getUrl() %>" id="url" disabled/>
				</p>

				<h3>Details</h3>
            <ul>
            <li><h4>Proposer Email</h4>
	            	<span id="proposerMail"><%=event.getProposerEmail() %></span>
	            </li>
					<li><h4>Create Time</h4>
	            	<span><%=event.getCreateTime().toLocaleString() %></span>
	            </li>
					<li id="closeTimeLi" hidden>
					<h4>Close Time</h4>
	            	<span id="closeTimeText"><%=event.getCloseTime().toLocaleString() %></span>
	            </li>
				</ul>

			</div>
			<div class="about-me">

         	<h3>About Us</h3>
		      <p>
		      <a href="http://www.cs.hku.hk/~fliang"><img src="images/hku.png" width="40" height="40" alt="firefox" class="float-left" /></a>
            We are an innovative and enthusiastic team from the CS department of The University of Hong Kong.   <a href="http://www.cs.hku.hk/~fliang">Learn more...</a>
				</p>

         </div>

      <!-- /sidebar -->
		</div>

    <!-- content -->
	</div>

<!-- /content-out -->
</div>

<!-- footer-bottom -->
<div id="footer-bottom">

	<p class="bottom-left">
		&copy; 2013 <strong>Copyright Info</strong>&nbsp; &nbsp; &nbsp;
		<a href="http://www.cs.hku.hk/~fliang" title="homepage">Hooku</a> from <a href="http://www.cs.hku.hk/~fliang">HKU</a>
	</p>

	<p class="bottom-right">
		<a href="index.html">Home</a> |
      <strong><a href="#top">Back to Top</a></strong>
   </p>

<!-- /footer-bottom-->
</div>

</body>
</html>
